package com.ruoyi.jihuo.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.jihuo.domain.JhTmUploadData;
import com.ruoyi.jihuo.service.IJhTmUploadDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据包Controller
 *
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/jihuo/data")
public class JhTmUploadDataController extends BaseController {
    @Autowired
    private IJhTmUploadDataService jhTmUploadDataService;

    /**
     * 查询数据包列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:list')")
    @GetMapping("/list")
    public TableDataInfo list(JhTmUploadData jhTmUploadData) {
        startPage();
        List<JhTmUploadData> list = jhTmUploadDataService.selectJhTmUploadDataList(jhTmUploadData);
        return getDataTable(list);
    }

    /**
     * 导出数据包列表
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:export')")
    @Log(title = "数据包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JhTmUploadData jhTmUploadData) {
        List<JhTmUploadData> list = jhTmUploadDataService.selectJhTmUploadDataList(jhTmUploadData);
        ExcelUtil<JhTmUploadData> util = new ExcelUtil<JhTmUploadData>(JhTmUploadData.class);
        util.exportExcel(response, list, "数据包数据");
    }

    /**
     * 获取数据包详细信息
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:query')")
    @GetMapping(value = "/{jhId}")
    public AjaxResult getInfo(@PathVariable("jhId") Long jhId) {
        return success(jhTmUploadDataService.selectJhTmUploadDataByJhId(jhId));
    }

    /**
     * 新增数据包
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:add')")
    @Log(title = "数据包", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JhTmUploadData jhTmUploadData) {
        return toAjax(jhTmUploadDataService.insertJhTmUploadData(jhTmUploadData, false));
    }

    /**
     * 修改数据包
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:edit')")
    @Log(title = "数据包", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JhTmUploadData jhTmUploadData) {
        return toAjax(jhTmUploadDataService.updateJhTmUploadData(jhTmUploadData));
    }

    /**
     * 删除数据包
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:remove')")
    @Log(title = "数据包", businessType = BusinessType.DELETE)
    @DeleteMapping("/{jhIds}")
    public AjaxResult remove(@PathVariable Long[] jhIds) {
        return toAjax(jhTmUploadDataService.deleteJhTmUploadDataByJhIds(jhIds));
    }

    /**
     * 导入数据包
     */
    @PreAuthorize("@ss.hasPermi('jihuo:data:import')")
    @Log(title = "导入数据包", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) {
        try {
            return success(jhTmUploadDataService.importData(file));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

}
